package com.hundsun.fcp.dao;

import java.util.List;

import android.annotation.TargetApi;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;

import com.hundsun.fcp.common.logger.Logger;
import com.hundsun.fcp.common.utils.StringUtils;
import com.hundsun.fcp.db.columns.MessageColumns;
import com.hundsun.fcp.db.columns.MineColumns;
import com.hundsun.fcp.openapi.trade.bean.ResponseLoginBean;

public class MessageDao {
	private static final Logger LOGGER = Logger.getLogger(MessageDao.class);
	private Context context;
	private BaseDao baseDao;
	
	public MessageDao(Context context){
		baseDao = new BaseDao(context);
		this.context = context;
	}
	
 
	public void clearAllMessage(){
		try {
			baseDao.openW().delete(MessageColumns.Message, null, null) ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private ResponseLoginBean cursorToMessage(Cursor cursor){
		ResponseLoginBean loginResult = new ResponseLoginBean();
		loginResult.setClientno(cursor.getString(cursor.getColumnIndex(MessageColumns.MESSAGE_ID))) ;
		loginResult.setClientname(cursor.getString(cursor.getColumnIndex(MessageColumns.MESSAGE_TITLE))) ;
		loginResult.setSex(cursor.getString(cursor.getColumnIndex(MessageColumns.MESSAGE_BODY))) ;
		loginResult.setBirthday(cursor.getString(cursor.getColumnIndex(MessageColumns.MESSAGE_READ))) ; 
		 
		return loginResult;
	}
	
	private List queryAllMessage(){
		return null ;
	}
	
	private ContentValues messageToContentValues (ResponseLoginBean loginResult){
		ContentValues contentValues = new ContentValues();
		contentValues.put(MessageColumns.MESSAGE_ID, loginResult.getClientno());
		contentValues.put(MessageColumns.MESSAGE_TITLE, StringUtils.handString(loginResult.getClientname()));
		contentValues.put(MessageColumns.MESSAGE_BODY, StringUtils.handString(loginResult.getShortname()));
		contentValues.put(MessageColumns.MESSAGE_READ, StringUtils.handString(loginResult.getSex())); 
		return contentValues;
	}
	
 
	@TargetApi(Build.VERSION_CODES.FROYO)
	public long insertMineResult(ResponseLoginBean loginResult){
		return  baseDao.openW().insertWithOnConflict(MessageColumns.Message, null, messageToContentValues(loginResult), SQLiteDatabase.CONFLICT_REPLACE);
	}
 
	
	/**
	 * 更新字段
	 * @param messageId
	 * @param columnName
	 * @param columnVaule
	 * @return
	 */
	public long updateMessageColumn(String messageId,String columnName,String columnVaule){
		ContentValues contentValues = new ContentValues();
		contentValues.put(columnName, columnVaule);
		return baseDao.openW().update(MessageColumns.Message, contentValues, MessageColumns.MESSAGE_ID +" =?", new String[]{messageId});
	}
}
